new Vue({
    el: '#app',
    data() {
        return {
            menuList: [],
            tableData: [],
            defaultProps: {
                children: 'Children',
                label: 'Model_Name'
            },
            User_Number: "",
            allCheckedKeys: [],
            loading: false,
            type: '',
            activeName: 'first',
            checkList: []
        }
    },
    mounted() {
        const User_Number = sessionStorage.getItem("User_Number");
        this.type = sessionStorage.getItem("type");
        this.User_Number = User_Number
        this.getData(this.type);
        this.setCheckMenu(User_Number);
    },
    computed: {
        CheckedKeys(){//已选中的页面节点
            let arr = []
            this.ergodic(this.menuList,arr);
            return  this.allCheckedKeys.filter(i=>arr.filter(j=>j.Url).some(j=>j.Model_Number==i))
        },
    },
    methods: {
        getData(type) {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/getAllFunctionMenu?UserName=&Password=&FunctionType=${type}`,
                type: 'GET',
                success: function (Result) {
                    that.menuList = Result; //将获取的数据存到表格数据中
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        ergodic(menuList, List){
            menuList.forEach(item=>{
                let map = deepClone(item)
                List.push(map);
                if (map.Children&&map.Children.length>0) {
                    this.ergodic(map.Children , List);
                }
                map.Children = [];
            })
        },
        submitForm() {
            const that = this;
            this.loading = true;
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
                type: 'POST',
                data: {
                    Data: that.type=='0'?"base_system_rights_user_model":'business_system_rights_user_model' ,
                    Condition: "User_Number='" + that.User_Number + "'"
                },
                success: function (Result) {
                    //1.修改权限
                    Result.forEach(item=>{
                        if (that.CheckedKeys.some(i=>i==item.Model_Number)) {
                            item.UR_New = '1'
                        }else{
                            item.UR_New = '0'
                        }
                    })
                    //2.添加权限
                    let system_rights_user_model = [];
                    that.CheckedKeys.forEach(Model_Number=>{
                        if (!Result.some(item=>item.Model_Number==Model_Number)) {
                            system_rights_user_model.push({
                                User_Model_Serial_Number:'',
                                Model_Number,
                                User_Number:that.User_Number,
                                UR_New:'1',
                            })
                        }
                    })
                    let data = {};
                    if (that.type=='0') {
                        data ={
                            Data:JSON.stringify({
                                Modify:{base_system_rights_user_model:Result},
                                Add:{base_system_rights_user_model:system_rights_user_model}
                            })
                        } 
                    } else if (that.type=='1'){
                        data ={
                            Data:JSON.stringify({
                                Modify:{business_system_rights_user_model:Result},
                                Add:{business_system_rights_user_model:system_rights_user_model}
                            })
                        }
                    }
                    $.ajax({
                        headers:requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data,
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("提交成功")
                                window.history.go(-1)
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    })
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        handleCheckChange() {
            let halfKeys = this.$refs.tree.getHalfCheckedKeys();
            let keys = this.$refs.tree.getCheckedKeys()
            this.allCheckedKeys = [...keys, ...halfKeys]
        },
        setCheckMenu(UserNumber) {
            const that = this
            $.ajax({
                headers: requestHeaders(),
                type: 'POST',
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                data: {
                    SQLSerialNumber: this.type == '0' ? 'A_base_system_rights_user_model' : 'A_business_system_rights_user_model',
                    Condition: window.btoa(unescape(encodeURIComponent(UserNumber))),
                    ExtensionCondition: '',
                },
                success: function (Result) {
                    let arr = [];
                    Result.forEach(i => {
                        arr.push(i.Model_Number)
                    })
                    that.handleCheckChange()
                    that.$refs.tree.setCheckedKeys(arr, true)
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
    }
})